<?php
require_once ($INCLUDE_PATH . "./include/WebService2Db.php");
class EmailModel {
//public $db_name; //"_email_number"
    public $db_name="";
	public function typeCount($pid,$start_time=0,$end_time=0,$url) {
        $s_time = ($start_time);
		$e_time = ($end_time);
		$table = PRE."_send_email";
        $DB=new DB_MYSQL;
		$domainlist = $url;
		$days = 20;
		if($start_time!=0 && $end_time!=0)
		{
			$ddd = $e_time - $s_time;
			$days = $ddd / (24*60*60);
			$days = (int)$days;
		}
		$one_day = 24*60*60;
		$today = strtotime(date('Y-m-d'));
		for($ii=0;$ii<count($domainlist);$ii++) {
            $domain = $domainlist[$ii];
        	$c[$i]['domain']=$domain;
			//SELECT * FROM `hh_send_email` WHERE instr(email,'@163.com')
			$d_where = '';
			for($j=$days;$j>=0;$j--)
			{
				if($start_time==0)
					$s_time = $today - $one_day*$j;
				else
					$s_time = $end_time - $one_day*$j;
				if($end_time==0)
					$e_time = $today - $one_day*($j-1);
				else
					$e_time = $end_time - $one_day*($j-1);
				//if($start_time!=0 && $end_time!=0)
					$d_where = " and send_time>{$s_time} and send_time<{$e_time}";
				
				$c[$i]['time'] = date('Y-m-d',$s_time);
				
				$domain_where = " and sendtype='{$domain}'". $d_where;
					$where = " where pid = '".$pid."' ".$domain_where; 
					$c[$i]['all'] = $DB->count_records($table,"id",$where,DB);
					$where = " where pid = ".$pid." and open_time IS NOT NULL".$domain_where;
					$c[$i]['nosend'] = $DB->count_records($table,"id",$where,DB);
					$where = " where pid = ".$pid." and click_time IS NOT NULL".$domain_where;
					$c[$i]['send'] = $DB->count_records($table,"id",$where,DB);
				$i++;
				}
		}
        return $c;
    }
	
	public function urlCount($pid,$start_time=0,$end_time=0,$url) {
		$s_time = ($start_time);
		$e_time = ($end_time);
		$table = PRE."_send_email";
        $DB=new DB_MYSQL;
		$domainlist = $url;
		$days = 20;
		if($start_time!=0 && $end_time!=0)
		{
			$ddd = $e_time - $s_time;
			$days = $ddd / (24*60*60);
			$days = (int)$days;
		}
		$one_day = 24*60*60;
		$today = strtotime(date('Y-m-d'));
		foreach ($domainlist as $key => $row) {
            $domain = $row;
        	$c[$i]['domain']=$domain;
			//SELECT * FROM `hh_send_email` WHERE instr(email,'@163.com')
			$d_where = '';
			for($j=$days;$j>=0;$j--)
			{
				if($start_time==0)
					$s_time = $today - $one_day*$j;
				else
					$s_time = $end_time - $one_day*$j;
				if($end_time==0)
					$e_time = $today - $one_day*($j-1);
				else
					$e_time = $end_time - $one_day*($j-1);
				//if($start_time!=0 && $end_time!=0)
					$d_where = " and send_time>{$s_time} and send_time<{$e_time}";
				
				$c[$i]['time'] = date('Y-m-d',$s_time);
				
				$domain_where = " and instr(email,'@{$domain}')". $d_where;
					$where = " where pid = '".$pid."' ".$domain_where; 
					$c[$i]['all'] = $DB->count_records($table,"id",$where,DB);
					$where = " where pid = ".$pid." and open_time IS NOT NULL".$domain_where;
					$c[$i]['nosend'] = $DB->count_records($table,"id",$where,DB);
					$where = " where pid = ".$pid." and click_time IS NOT NULL".$domain_where;
					$c[$i]['send'] = $DB->count_records($table,"id",$where,DB);
				$i++;
				}
		}
        return $c;
    }
	
    public function emailCount($pid,$start_time=0,$end_time=0,$days=0) {
        $table = PRE."_send_email";
        $DB=new DB_MYSQL;
		
		//$start_time = (int)$s_time;
		//$start_time = (int)$e_time;
		
		if($days!=0)
		{
			if($start_time!=0 && $end_time!=0)
			{
				$ddd = $end_time - $start_time;
				$days = $ddd / (24*60*60);
				$days = (int)$days;
			}
			$one_day = 24*60*60;
			$today = strtotime(date('Y-m-d'));
			for($i=$days;$i>=0;$i--)
			{
				if($start_time==0)
					$s_time = $today - $one_day*$i;
				else
					$s_time = $end_time - $one_day*$i;
				if($end_time==0)
					$e_time = $today - $one_day*($i-1);
				else
					$e_time = $end_time - $one_day*($i-1);
				$c[$i]['time'] = date('Y-m-d',$s_time);
				$d_where = " and send_time>{$s_time} and send_time<{$e_time}";
				$where = " where pid = '".$pid."'".$d_where;
				$c[$i]['all'] = $DB->count_records($table,"id",$where,DB);
				$where = " where pid = ".$pid." and open_time IS NOT NULL".$d_where;
				$c[$i]['nosend'] = $DB->count_records($table,"id",$where,DB);
				$where = " where pid = ".$pid." and click_time IS NOT NULL".$d_where;
				$c[$i]['send'] = $DB->count_records($table,"id",$where,DB);
			}
		}
		else
		{
			
				$where = " where pid = '".$pid."' ";
				$c['all'] = $DB->count_records($table,"id",$where,DB);
				$where = " where pid = ".$pid." and open_time IS NOT NULL";
				$c['nosend'] = $DB->count_records($table,"id",$where,DB);
				$where = " where pid = ".$pid." and click_time IS NOT NULL";
				$c['send'] = $DB->count_records($table,"id",$where,DB);
			
		}
        return $c;
    }
    public function saveEmailInfo($s) {
    //$s1 = 'id';
    //$s2 = "'".$s['id']."'";
        foreach ($s as $key => $ss) {
            if($key != 'id') {
                $s1 .= ",".$key;
                $s2 .= ",'".$ss."'";
            }
        }
        $s1 = substr($s1,1);
        $s2 = substr($s2,1);
        $DB=new DB_MYSQL;
        $sql="insert into `".PRE.$this->db_name."`(".$s1.")" ."VALUES(".$s2.")";

        //  $sql="SELECT *  FROM `hh_user` WHERE `account` = '".$username."' AND `password` = '".$password."'";
        $query = $DB->insert($sql,DB);
        //  echo $DB->insert_id;
        return $DB->insert_id;
    }
    public function editEmailInfo($id,$s) {

        foreach ($s as $key => $ss) {
                $s1 .= ",".$key."='".$ss."'";
        }
        $s1 = substr($s1,1);
        $DB=new DB_MYSQL;
        $sql="update  `".PRE.$this->db_name."` set ".$s1.
            " where id=".$id;
        //  $sql="insert into `".PRE."_user`(".$s1.")" ."VALUES(".$s2.")";

        //  $sql="SELECT *  FROM `hh_user` WHERE `account` = '".$username."' AND `password` = '".$password."'";
        $query = $DB->update($sql,DB);
        //  echo $affected_rows;
        return $DB->affected_rows;
    }
    public function getEmailInfo($id) {
        $DB=new DB_MYSQL;
        $DB->connect();
        $sql="SELECT *  FROM `".PRE.$this->db_name."` WHERE `id` = ".$id."";
        $query = $DB->query_first($sql,DB);
        return $query;
    }

    public function getListCount($where="") {
    //$where = " where state = 1";
        $DB=new DB_MYSQL;
        $table=PRE.$this->db_name;
        $sum=$DB->count_records($table,"id",$where,DB);
        return $sum;

    // echo $sum;
    }
    public function getList($page,$liststart,$pageSize,$where="") {
        $DB=new DB_MYSQL;
        $sql="SELECT *  FROM `".PRE.$this->db_name."` ".$where." LIMIT ".$liststart.",".$pageSize;
        $query = $DB->fetch_array($sql,DB);
        return $query;

    }
    public function delUser($id) {
        $DB=new DB_MYSQL;
        $sql = " delete from ".PRE.$this->db_name." where id=".$id;
        $query = $DB->delete($sql, DB);
    }

	public function addUpload($s) {
 		foreach ($s as $key => $ss) {
            if($key != 'id') {
                $s1 .= ",".$key;
                if ($key == "send_time"){
                $s2 .= ",UNIX_TIMESTAMP('".$ss."')";
                }else{
                   $s2 .= ",'".$ss."'";
                }
            }
        }
        $s1 = substr($s1,1);
        $s2 = substr($s2,1);
        $DB=new DB_MYSQL;
        $sql="insert into `".PRE.$this->db_name."`(".$s1.")" ."VALUES(".$s2.")";

        //  $sql="SELECT *  FROM `hh_user` WHERE `account` = '".$username."' AND `password` = '".$password."'";
        $query = $DB->insert($sql,DB);
        //  echo $DB->insert_id;
        return $DB->insert_id;    
	}
	
	/*public function treatFile($folder)
	{
		$arrayList = array();
		//��Ŀ¼    
		$fp=opendir($folder);    
		//�Ķ�Ŀ¼    
		while(false!=$file=readdir($fp))    
		{    
		//�г������ļ���ȥ��'.'��'..'    
			if($file!='.' &&$file!='..')    
			 {    
				//$file="$folder/$file";    
				$file="$file";     
				//��ֵ������    
				$arr_file[]=$file;     
		   
				 }    
		}    
		//�����    
		if(is_array($arr_file))    
		{    
			
			while(list($key,$value)=each($arr_file))    
			 {    
				//echo "$key=>$value<br/>";   
				$file_handle = fopen($folder.$value, "r"); 
				  while (!feof($file_handle)) { 
					  $line = fgets($file_handle); 
					  $arrayList[] = $line;
				  } 
				  fclose($file_handle); 
			 }     
		   
		}     
		//�ر�Ŀ¼     
		closedir($fp);  
		return $arrayList;
	}*/
	
	public function treatFile($arr_file)
	{
		$arrayList = array();
		//��Ŀ¼    
		//$fp=opendir($folder);    
		//�Ķ�Ŀ¼    

		$file_handle = fopen($arr_file, "r"); 
		while (!feof($file_handle)) { 
			  $line = fgets($file_handle); 
			  $arrayList[] = $line;
		  } 
		fclose($file_handle);    
		   
		 
		return $arrayList;
	}
	
	//删除指定目录（文件夹）中的所有文件函数
	function removeDir($dirName) 
	{ 
		if(! is_dir($dirName)) 
		{ 
	   @unlink($dirName);
			return false; 
		} 
		$handle = @opendir($dirName); 
		while(($file = @readdir($handle)) !== false) 
		{ 
			if($file != '.' && $file != '..') 
			{ 
				$dir = $dirName . '/' . $file; 
				is_dir($dir) ? removeDir($dir) : @unlink($dir); 
			} 
		} 
		closedir($handle); 
		 
		return rmdir($dirName) ; 
	} 
}

?>
